<?php 
session_start();
require_once ("class/m_card_coupon.php");

	$m = new M_card_coupon();
	$user=$_SESSION['username'];
	
	if (isset($_POST['m'])  &&  $_POST['m']!=""){
		if ($_POST['m']== 'detail'){
			if(isset($_POST['id']) && $_POST['id']!=""){ 
				$id=$_POST['id'];
				//$id=1;
				echo json_encode( $m->getDataDetail($id) );
			}else{
				echo "Errro";
			}
		}else if ($_POST['m']== 'add'){
			$ar= $_POST['var'];
			$ar["CreatedBy"]=$user;
			$ar["CreatedDate"]=date( "Y/m/d H:i:s" );
			
			//convert date from y/m/d to d/m/y
			list($day, $month, $year) = split('/', $ar["EffectiveDateFrom"]);
			$ar["EffectiveDateFrom"]=$year."/".$month."/".$day;
			
			list($day, $month, $year) = split('/', $ar["EffectiveDateTo"]);
			$ar["EffectiveDateTo"]=$year."/".$month."/".$day;
			
			$id=$m->InsertData($ar);
			
			$result=$m->getDataDetail($id);
			
			echo "<tr>".genTable($result , $_POST['rows']+1)."</tr>";
			
		}else if ($_POST['m']=='edit'){
			if (isset($_POST['var'])){
				$ar=$_POST['var'];
				$flag=isset($ar);

				$ar["UpdatedBy"]=$user;
				$ar["UpdatedDate"]=date( "Y/m/d H:i:s" );
				 
				//convert date from y/m/d to d/m/y
				list($day, $month, $year) = split('/', $ar["EffectiveDateFrom"]);
				$ar["EffectiveDateFrom"]=$year."/".$month."/".$day;
				
				list($day, $month, $year) = split('/', $ar["EffectiveDateTo"]);
				$ar["EffectiveDateTo"]=$year."/".$month."/".$day;
			
				$sqlScript="select * from tb_coupon where IsDeleted=0 and ID='". $_POST['id'] ."'";
				$m->updateData($sqlScript, $ar);
				
				$result=$m->getDataDetail($_POST['id']);
			 
				echo genTable($result, $_POST['rows']);
			}
					
		}else if ($_POST['m']=='del'){
			if(isset($_POST['var'])){
				$o=$_POST['var'];
				$m->delData($o["id"]);
				
			}
		}else if ($_POST['m']== 'search'){
			$name=$_POST['name'];
			$type=$_POST['type'];
			$page=$_POST['page'];
			$page=isset($page)?$page:1;	
					
			$result=$m->getData($name,$type,$page);
			$rows=$m->getRowCount();
 
			$i=1 + (($page-1) * $m->getListPage());
			$html="";
			if ($rows>0){
				while($row = $result->FetchRow()){
					If($i%2 <> 0){
						$html .= "<tr class=row1>";
					} else{
						$html .= "<tr>";
					}
					$html .= genTable( $row ,$i );
					$html .= "</tr>";
					$i++;
				}
			}else{
				$html="<tr>"
					."<td colspan=7 align='center' ><img src='./images/info.gif'/><b/></td>"
					."</tr>";
			}
			
			//Return result , rows , page
			$ar_result=array('data'=>$html,
							'rows'=>$rows,
							'totalpage'=>$m->getTotalPage());
			echo json_encode( $ar_result );
		}
	}else{echo"Error2";}

	function genTable($row,$i){
		global $card_couponScreen;
		$s= "<td align='center'>".$i."</td>";
		$s.= "<td style='display:none'>".$row["ID"]."</td>";
		$s.= "<td noWrap align='center'>".$row["Name"]."</td>" ;
		$s.="<td noWrap align='center'>".$row["Type_Card"]."</td>" ;
		$s.="<td noWrap align='center'>".$row["Discount"].' '.$row["Type_Dis"]."</td>" ;
		$s.="<td noWrap align='center'>".$row["effecFrom"]." - ".$row["effecTo"]."</td>" ;
		
		if ($_SESSION["permission"][$card_couponScreen]['Edit']=='1') 		
			$s.="<td noWrap align='center'><img src='./images/edit.png' class='btnEdit' style='cursor:pointer' title='Edit' border='0'/></td>";
		else 
			$s.="<td>&nbsp;</td>";
			
		if ($_SESSION["permission"][$card_couponScreen]['Deleted']=='1') 		
			$s.="<td align='center'><img src='./images/remove.png' class='btnDel' style='cursor:pointer' title='Delete' border='0'/></td>" ;		
		else 
			$s.="<td>&nbsp;</td>";
			
		return $s;
	}

?>